<?php

namespace app\common\lib;

use Endroid\QrCode\QrCode as QrCodeBus;
use Endroid\QrCode\Writer\PngWriter;

class QrCode
{
    public function generate($data)
    {
        $qrCode = QrCodeBus::create($data);
        $writer = new PngWriter();
        $result = $writer->write($qrCode);
        // 指定保存路径
        $filePath = public_path() . '/storage/qrCode/qr_code.png';
        file_put_contents($filePath, $result->getString());
        // 返回成功信息
        return 'QR Code has been generated and saved to ' . $filePath;
    }
}
